﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RabbitMQ_Consumer.Noraml
{
    public class Receive
    {
        public static void ReceiveMessage()
        {
            // 消费者消费是队列中消息
            string queueName = "normal";
            //1.建立链接链接
            var connection = RabbitMQHelper.GetConnection();
            {
                //2.建立信道
                var channel = connection.CreateModel();
                {
                    //3.声明队列：如果你先启动是消费端就会异常
                    channel.QueueDeclare(queueName, false, false, false, null);
                    //4.创建一个消费者实例
                    var consumer = new EventingBasicConsumer(channel);
                    //5.绑定消息接收后的事件委托
                    consumer.Received +=(model, ea) => {
                        var message = Encoding.UTF8.GetString(ea.Body.ToArray());
                        Thread.Sleep(1000);
                        Console.WriteLine(" Normal Received => {0}", message);
                    }; 
                    //6.启动消费者
                    channel.BasicConsume( queue: queueName, autoAck:true, consumer);//开始消费
                }
               
            }
          
        } 
    }
}
